import { BOX_INIT } from "./Model/Box";
import { BUILDING } from "./Model/Building";
import {Item, EQUIP, STATE} from "./Model/Model"

class PlayerData {
    box = BOX_INIT;
    building = BUILDING
    state = new STATE()
    equip = new EQUIP()

    //设置状态
    stageChange(list:STATE){
        this.state.changeState(list)
        this.checkDeath()
    }

    // 体温状态
    getTempDesc():string{
        var playerState = this.state;
        var state = playerState.temp;
        if(state>30)return 'veryHot';
        if(state>20)return 'hot';
        if(state>10)return 'warm';
        if(state>0)return 'nice';
        if(state>-10)return 'nice';
        if(state>-20)return 'cool';
        if(state>-30)return 'cold';
        return 'veryCold';
    }
    // 死因
    handleDeath(type:string){
        var reason = '';
        switch(type){
            case'frozen':
            reason = '寒冷';
            break;
            case'hunger':
            reason = '饥荒';
            break;
            case'thirsty':
            reason = '脱水';
            break;
            case'health':
            reason = '失血过多';
            break;
        }
        // 设置状态
        // 游戏结束
    }

    //检查死亡
    checkDeath(){
        var playerState = this.state
        if(playerState.hp <= 0){
            if(playerState.ps == 0 && (this.getTempDesc() == 'cold' ||  this.getTempDesc() == 'veryCold')){
                this.handleDeath('frozen');
            }
            this.handleDeath('health');
        }
        if(playerState.full <= 0){
            this.handleDeath('hunger');
        }
        if(playerState.moist <= 0){
            this.handleDeath('thirsty');
        }
    }

    //装备
    takeOnEquip(item:Item){
        //背包到装备
        var name = item.name
        //判断装备位置
        var category = "foot"
        var temp:Item = this.equip["body"]
        // if(temp != null){
        //     this.box.bag.things[temp.name] = (this.box.bag.things[temp.name] || 0) +1
        // }
        // this.equip["body"] = item
        // this.box.bag.things[name] -= item.amount
        // if(this.box.bag.things[name] <= 0){
        //     delete this.box.bag.things[name]
        // }
    }
    takeOffEquip(item:Item){
        var nowsize = Object.getOwnPropertyNames(this.box.bag.things).length
        var name = item.name
        // if(this.box.bag.things[name] == undefined && nowsize >= this.box.bag.size){
        //     //背包没有空格子
        //     return
        // }
        // this.box.bag.things[name] = (this.box.bag.things[name] || 0) +1
    }

    //攻击力
    getAttack = function():number{
        return 0
    }
    //收到伤害
    getDamage = function(damage:number):number{
        //闪避
        return damage
    }

    //拾取物品,返回剩下物品
    pickupItems(items:Item[]):Item[]{
        var leftItems:Item[] = []
        for (const key in items) {
            var item = items[key]
            var nowsize = Object.getOwnPropertyNames(this.box.bag.things).length
            // if(this.box.bag.things[item.name] == undefined && nowsize >= this.box.bag.size){
            //     // 背包放不下了
            //     leftItems.push(item)
            // }else{
            //     this.box.bag.things[item.name] += (this.box.bag.things[item.name] || 0) + item.amount
            // }
        }
        return leftItems
    }
    //丢物品
    dropItems(items:Item[]):void{
        for (const key in items) {
            var item = items[key]
            // if(this.box.bag.things[item.name] != undefined){
            //     this.box.bag.things[item.name] -= item.amount
            //     if(this.box.bag.things[item.name] <= 0){
            //         // 删除物品
            //         delete this.box.bag.things[item.name]
            //     }
            // }
        }
    }

    //使用物品
    useItem(item:Item):void{

    }

    // 放入大箱子
    putinBox(items:Item[]):Item[]{
        var leftItems:Item[] = []
        for (const key in items) {
            var item = items[key]
            var nowsize = Object.getOwnPropertyNames(this.box.bigBox.things).length
            // if(this.box.bigBox.things[item.name] == undefined && nowsize >= this.box.bigBox.size){
            //     // 箱子放不下了
            //     leftItems.push(item)
            // }else{
            //     this.box.bigBox.things[item.name] += (this.box.bigBox.things[item.name] || 0) + item.amount
            // }
        }
        return leftItems
    }

    //大箱子拿出
    takeoutBox(items:Item[]):void{
        for (const key in items) {
            var item = items[key]
            var nowsize = Object.getOwnPropertyNames(this.box.bag.things).length
            // if(this.box.bag.things[item.name] == undefined && nowsize >= this.box.bag.size){
            //     // 背包放不下了
            //     break;
            // }else{
            //     this.box.bag.things[item.name] += (this.box.bag.things[item.name] || 0) + item.amount
            //     if(this.box.bigBox.things[item.name] != undefined){
            //         this.box.bigBox.things[item.name] -= item.amount
            //         if(this.box.bigBox.things[item.name] <=0){
            //             delete this.box.bigBox.things[item.name]
            //         }
            //     }
            // }
        }
    }
}

export let playerData:PlayerData = new PlayerData()